package com.bcpapp.project.utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class TimeUtils {

    public static String[] getTwoTime(int min) {
        Calendar beforeTime = Calendar.getInstance();
        Date nowtime = beforeTime.getTime();
        String nowtime1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(nowtime);  // 前2分钟时间

        beforeTime.add(Calendar.MINUTE, -min);// 2分钟之前的时间
        Date beforeD = beforeTime.getTime();
        String before2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(beforeD);  // 前2分钟时间
        String[] result = {nowtime1, before2};
        return result;
    }

    public static Integer[] getTwoHour()  {
        try {
        //getInstance()将返回一个Calendar的对象。
        Calendar calendar = Calendar.getInstance();
        //设置小时
        calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY) - 1);
        //格式化时间
        SimpleDateFormat df1 = new SimpleDateFormat("yyyyMMddHH");
        //打印出时间
        String lastHour = df1.format(calendar.getTime());
        String nowHour = df1.format(new Date());

        String lHour =  lastHour+"0000";
        String nHour =  nowHour+"0000";

        DateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");

        Date  date = df.parse(lHour);
        Date date1 = df.parse(nHour);


        Calendar cal = Calendar.getInstance();
        Calendar cal1 = Calendar.getInstance();

        cal.setTime(date);
        cal1.setTime(date1);

        long tim = cal.getTimeInMillis();
        long tim1 = cal1.getTimeInMillis();

        System.out.println(tim);
        System.out.println(tim1);
            String ts1 = String.valueOf(tim).substring(0, 10);
            String ts2 = String.valueOf(tim1).substring(0, 10);
            System.out.println("ts1-----"+ts1);
            System.out.println("ts2-----"+ts2);

            Integer[] result = {Integer.parseInt(ts1 ),Integer.parseInt(ts2) };

        return result;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String getDateFormat() {
        long time = System.currentTimeMillis();
        String nowtime1 = new SimpleDateFormat("yyyyMMdd-HHmmss").format(time);

        return nowtime1;
    }
    public static String getDateFormatMM() {
        long time = System.currentTimeMillis();
        String nowtime1 = new SimpleDateFormat("yyyyMMdd-HHmm").format(time);

        return nowtime1;
    }

    public static String getDateStr() {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd ");
        Date date = new Date(System.currentTimeMillis());
        System.out.println(formatter.format(date));
        return date.toString();
    }

    public static String getLastDateStr() {
        Date today = new Date(System.currentTimeMillis() - 1000 * 60 * 60 * 24);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String yesterday = simpleDateFormat.format(today);//获取昨天日期
        System.out.println(yesterday);
        return  yesterday;
    }
}
